import getRequest from '@/utils/request.js'
import localStorage from '@/actions/localStorage.js'
// 获取公共参数
function getParmas ($this,data) {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  var token1
  if (currentPage.token) {
    token1 = currentPage.token
  } else {
    token1 = ''
  }
  // const token1 = $this.$route.query.token;
  const token2 = localStorage.getStorage('token')
  const token = token1 || token2;
  const time = Date.parse(new Date())/1000;
  const data1 = {
    token: token,
    time: time,
    device_id: '1532658064',
    version: '1.0.1',
    platform: 'mini_program'
  }
  const dataAll=Object.assign(data1,data)
  return dataAll
}
// 微信登录
function getWxLogin ($url, method, data, header, isToLogin, $this) {
  var indexData
  const dataAll = getParmas($this, data)
  const async = new Promise((resolve) => {
    indexData = getRequest.getRequest($url, method, dataAll, header, isToLogin)
    resolve(indexData)
  })
  Promise.all([async])
    .then((res) => {
      console.log(indexData)
      wx.setStorageSync('session_key', indexData._v.data.session_key );
      wx.setStorageSync('openid', indexData._v.data.wx_openid || indexData._v.data.openid );
      wx.setStorageSync('unionid', indexData._v.data.unionid);
      if(indexData._v.data.token){
        wx.setStorageSync('token', indexData._v.data.token);
      }
    })
}
// 主页
function index ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data=indexData._v.data;
      $this.isLogin2=true;
      $this.category_id=$this.data.category[0].category_id;
    });
}
// 主页商品
function getGoodsByCid ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this, data)
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.allPage=indexData._v.data.all_page;
      $this.isLoad=true;
      $this.list=$this.list.concat(indexData._v.data.list);
    });
}
// 我的个人信息页面
function mine ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data1=indexData._v.data;
      $this.is_login = indexData._v.code;
    });
}
// 订单列表
function orderList ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.allCount = indexData._v.data.all_count;
      $this.data=indexData._v.data.list;
      /*console.log($this.allCount);*/
      /*  $this.data1=indexData._v.data;
      $this.is_login = indexData._v.code;*/
      /*console.log($this.allCount)*/
    });
}
// 获取商品详情
function getProductsDetail ($url, method, data, header, isToLogin, $this) {
  var indexData
  const dataAll = getParmas($this, data)
  const async = new Promise((resolve) => {
    indexData = getRequest.getRequest($url, method, dataAll, header, isToLogin)
    resolve(indexData)
  })
  Promise.all([async])
    .then( (res) => {
      const data = indexData._v.data
      $this.needsGoods = indexData._v
      $this.goodsInfo = indexData._v.data
      $this.activitys_id = indexData._v.data.activity.activitys_id
      $this.swipeImg = data.goods_imgs
      $this.coupons = data.coupon;
      $this.time = data.time*1000;
      $this.isNew = data.is_new;
      $this.limit = data.limit;
      $this.activity = data.activity;
      $this.limitType = data.limit.limit_type;
      $this.group = data.group;
      $this.presale = data.is_presale;
      $this.buyLimit = data.limit.buy_limit;
      $this.is_join_group = data.group.is_join_group;
      $this.promise = data.promise;
      $this.type = data.type;
      $this.is_notice=data.is_notice;
      $this.textContent = indexData._v.data.member_tip;
      $this.cartNumber = indexData._v.data.cart_number;
      console.log($this.cartNumber);
      if ( $this.cartNumber>99){
        $this.cartNumber = "99+"
      }
      getpersonInformation("api/user/index","post",{},{},false,$this)
      getGoodsSpec("api/goods/getSpec","post",{
        group_id:$this.group_ids,
        limit_id:$this.limit_ids,
        goods_id:$this.goods_ids,
        activity_id:$this.activity_id,
      },{},false,$this)
      /*console.log($this.goodsInfo);*/
    });
}
// 解码手机号
function getTel ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      console.log(indexData._v.data)
      $this.data=indexData._v.data;
      $this.phone=JSON.parse($this.data).phoneNumber;
      console.log($this)
    });
}
// 获取验证码
function getCode ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      console.log(indexData._v.data)
      $this.data=indexData._v.data;
      $this.$set($this.phone, JSON.parse($this.data).phoneNumber)
      // $this.phone=JSON.parse($this.data).phoneNumber;
      console.log($this.phone)
    });
}
// 登录
function login ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      if(indexData._v.code===1){
        wx.setStorageSync("token",indexData._v.data.token)
        wx.switchTab({
          url:'../../pages/mine/main'
        })
      }else{
        wx.showToast({
          title: '手机号或验证码有误',
          icon: 'none',
          duration: 1000,
          mask:true
        })
      }
    });
}
// 所有商品
function allGoods($url, method, data, header, isToLogin, $this){
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data=indexData._v.data;
      $this.goodList=$this.goodList.concat($this.data.list);
      $this.allPage=$this.data.all_page;
    });
}
//获取分类列表
function categoryList($url, method, data, header, isToLogin, $this){
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.categoryList=indexData._v.data.list;
      let category_id;
      if(indexData._v.data.list[0].info){
        category_id=indexData._v.data.list[0].info[0].id
      }else{
        category_id=indexData._v.data.list[0].id
      }
      getGoodsByCid('api/Shop/getGoodsByCid', 'post', {category_id:category_id,page:1}, {}, false,$this)
    });
}
// 看更多团购信息
function seeMore ($url, method, data, header, isToLogin, $this) {
  console.log(2222)
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      console.log(indexData._v.data)
      $this.groupList = indexData._v.data.ret;
    });
}
//获取个人信息
function getpersonInformation ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.isNone=true;
      $this.is_login=indexData._v.code
      $this.data1=indexData._v.data;
    });
}
//获取商品规格
function getGoodsSpec ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.is_join_group=1;
      $this.respon = indexData._v.data;
      $this.spec =indexData._v.data.spec;
      $this.spec.forEach((v,i)=>{
        $this.labelNum.push('')
      })
      $this.sku = indexData._v.data.sku;
      $this.storeNums = indexData._v.data.all_store_nums;
      console.log($this.respon);
    });
}
//购买商品
function buyGoods ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ( (resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      let data = indexData._v;
      if (data.code === 1){
        localStorage.setStorage('returnArr',data)
        localStorage.setStorage('activity',$this.activity_id)
        localStorage.setStorage('goods_id',dataAll);
        wx.navigateTo({
          url:'../order/main'
        })
      } else if (data.code === -1){
        wx.showToast({
          title:data.message,
          icon:'none',
          duration:2000
        })
      }

    });
}
/*加入购物车*/
function joinChart ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      const data = indexData._v;
      $this.mesChart = indexData._v.message;
      console.log(indexData._v.data);
      wx.showToast({
        title: indexData._v.message,
        icon: 'none',
        duration: 2000
      })
      $this.goodsInfo.cart_number=data.cart_number;
    });
}
/*获取购物车商品*/
function cartList ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.isNone=true;
      $this.data=indexData._v.data;
      $this.list=$this.data.list;
      $this.isGet = true;
      $this.list.forEach((v,i)=>{
        $this.cartId.push(v.cart_id)
      })

    });
}
//修改购物车数量
function updateCart ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      if(res[0].code==1){
        $this.data.list.forEach((v,i)=>{
          if(data.cart_id==v.cart_id){
            if(data.type==1){
              v.goods_num++;
            }else{
              v.goods_num--;
            }
          }
        })
        $this.sumAllmoney();
      }
    });
}
//删除购物车商品
function delCart ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      if (indexData._v.code = 1) {
        wx.showToast({
          title:indexData._v.message,
          icon:'none',
          duration:2000
        })
        cartList('api/goods/cartList','post',{},{},true,$this);
      }

    });
}
//订单列表待评论列表
function commentList ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.commentList = indexData._v.data.list;
      $this.commentCount=indexData._v.data.all_count;
    });
}
//订单详情
function orderDetail ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data = indexData._v.data;
      $this.operate =  indexData._v.data.operate;
      console.log($this.operate);
    });
}
//获取订单相关信息
function getOrderPrice ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.pay =indexData._v.data.pay_price;
      $this.address_id =indexData._v.data.address.address_id;
      $this.shipping_fee = indexData._v.data.shipping_fee.freight;
    });
}
//获取支付方式
function getPayMethod ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.radio= indexData._v.data.pay_id;
      $this.dataList =indexData._v.data.list;
    });

  const content = wx.getStorageSync('goods_id');
  content['coupon_record_id'] = 0;
  content['address_id'] = $this.address_id;
  content['postscript'] = $this.postscript;
  content['self_lifting'] = 0;
  confirmOrder('api/order/confirmOrder','post', content,{},true,$this)
}
//获取订单列表支付方式
function getListPayMethod ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.radio= indexData._v.data.pay_id;
      $this.dataList =indexData._v.data.list;
      $this.finishOrder = true;
      console.log($this.dataList);
    });

 /* const content = wx.getStorageSync('goods_id');
  content['coupon_record_id'] = 0;
  content['address_id'] = $this.address_id;
  content['postscript'] = $this.postscript;
  content['self_lifting'] = 0;
  confirmOrder('api/order/confirmOrder','post', content,{},true,$this)*/
}
//下单
function getPay ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      if(indexData._v.code===1){
        if(indexData._v.data.pay_type==2){
          wx.requestPayment({
            timeStamp:indexData._v.data.wxpay_params.timeStamp,
            nonceStr:indexData._v.data.wxpay_params.nonceStr,
            package:indexData._v.data.wxpay_params.package,
            signType:indexData._v.data.wxpay_params.signType,
            paySign:indexData._v.data.wxpay_params.paySign,
            success:function(res){
              $this.shows=true;
            },
            fail:function(){
            },
          })
        }else{
          $this.shows=true;
        }

       /* wx.navigateTo({
          url:'../orderList/main?active=2'
        })*/
      }else if (indexData._v.code===-1) {
        $this.ispay=false;
        wx.showToast({
          title: indexData._v.message,
          icon: 'none',
          duration: 2000
        })
      }
    });
}
//确认订单
function confirmOrder ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.isGetMethod=false;
      $this.order_sn=indexData._v.data;
      console.log($this.order_sn);
    });
}
//热门搜索
function hotSearchTopTen ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data=indexData._v.data
    });
}
//根据关键词搜索商品
function searchGoods ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data=indexData._v.data;
      /*$this.searchList=$this.searchList.concat(indexData._v.data.list);*/
      $this.searchList=indexData._v.data.list;
      $this.allPage=indexData._v.data.all_page;
      if(wx.getStorageSync('searchGoods')){
        var searchGoods=JSON.parse(wx.getStorageSync('searchGoods'));
        searchGoods.unshift($this.searchContent);
        searchGoods=new Set(searchGoods);
        window.localStorage.setItem('searchGoods',JSON.stringify(searchGoods));
        $this.nearest= JSON.parse(wx.getStorageSync('searchGoods'));
      }else{
        wx.setStorageSync('searchGoods',JSON.stringify([$this.searchContent]));
        $this.nearest=[$this.searchContent];
      }
      $this.loading=true;
    });
}
//地址列表
function addressList ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.data=indexData._v.data;
      console.log(indexData._v.data);
      if(indexData._v.data.list.length>0){
        indexData._v.data.list.forEach((v,i)=>{
          $this.list.push({
            id:v.address_id,
            name:v.accept_name,
            tel:v.phone,
            address:v.big_address+v.address,
            smallAddress:v.address,
            area:v.area,
            is_default:v.is_default,
            first_id:v.province,
            second_id:v.city,
            third_id:v.area,
          })
        })
        $this.chosenAddressId=res.data.data.list[0].address_id
      }
})
}
/*添加收货地址*/
function addAddress ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      wx.navigateBack();
    });
}
/*选择默认收货地址*/
function setDefaultAddress ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      wx.navigateBack();
    });
}
/*订单操作*/
function cancelOrder ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      if (indexData._v.code === 1) {
        wx.showToast({
          title:indexData._v.message ,
          icon: 'none',
          duration: 2000
        });
        orderList("api/user/orderList","post",{status:$this.active,page:1},{},true,this);
      }

    });
}
//查询快递
function lookUpExpress ($url, method, data, header, isToLogin, $this) {
  var indexData;
  const dataAll = getParmas($this,data);
  const async = new Promise ((resolve) => {
    indexData = getRequest.getRequest ( $url, method, dataAll, header, isToLogin );
    resolve(indexData)
  });
  Promise.all([async])
    .then( (res) => {
      $this.express=res;

    });
}
export default{
  getListPayMethod,
  cancelOrder,
  setDefaultAddress,
  addAddress,
  addressList,
  searchGoods,
  hotSearchTopTen,
  confirmOrder,
  getPay,
  getPayMethod,
  getOrderPrice,
  getWxLogin,
  getParmas,
  index,
  getGoodsByCid,
  mine,
  orderList,
  getProductsDetail,
  getTel,
  getCode,
  login,
  allGoods,
  categoryList,
  getpersonInformation,
  seeMore,
  getGoodsSpec,
  buyGoods,
  joinChart,
  cartList,
  updateCart,
  delCart,
  commentList,
  orderDetail,
  lookUpExpress
}
